class Admin::ProductsController < ApplicationController
  layout 'admin'

  before_filter :authenticate_user!, :check_if_admin
  before_filter :get_categories, :only => [:new, :create, :edit, :update, :show]
  before_filter :get_brands, :only => [:new, :create, :edit, :update, :show]
  before_filter :get_products_at_home_count, :only => [:new, :create]
  #before_filter :get_colors, :only => [:new, :create, :edit, :update, :show]

  def index
    @products = Product.unscoped.not_deleted.paginate(:page => params[:page], :per_page => 50)

    respond_to do |format|
      format.html
    end
  end

  def show
    @product = Product.unscoped.find(params[:id])

    respond_to do |format|
      format.html
    end
  end

  def new
    @product = Product.new
    @product.product_colors.build

    # SI HAY 3 O MAS PRODUCTOS EN LA HOME, NO SE LE MUESTRA LA OPCION PARA AGREGARLO.

    #if !@products_at_home.blank? && @products_at_home

    respond_to do |format|
      format.html
    end
  end

  def create
    @product = Product.new(params[:product])

    unless params['categories'].blank?
      params['categories'].each do |category_id|
        c = Category.find(category_id)
        @product.categories << c
      end
    end

    unless params['colors'].blank?
      params['colors'].each do |color_id|
        c = Color.find(color_id)
        @product.colors << c
      end
    end

    respond_to do |format|
      if @product.valid?
        @product.save

        format.html { redirect_to admin_products_url }
      else
        format.html { render :new }
      end
    end
  end

  def edit
    @product = Product.unscoped.find(params[:id])

    # SI HAY 3 O MAS PRODUCTOS EN LA HOME, Y ESTE PRODUCTO NO ESTA, NO SE LE MUESTRA LA OPCION PARA AGREGARLO.
    @products_at_home = Product.at_home.select { |p| p.id != @product.id }.count

    respond_to do |format|
      format.html
    end
  end

  def update
    @product = Product.unscoped.find(params[:id])

    @product.update_categories(params['categories']) unless params['categories'].blank?
    #@product.update_colors(params['colors']) unless params['colors'].blank?

    # SI HAY 3 O MAS PRODUCTOS EN LA HOME, Y ESTE PRODUCTO NO ESTA, NO SE LE MUESTRA LA OPCION PARA AGREGARLO.
    @products_at_home = Product.at_home.select { |p| p.id != @product.id }.count

    respond_to do |format|
      if @product.valid? && @product.update_attributes(params[:product])
        format.html { redirect_to admin_products_url }
      else
        format.html { render :new }
      end
    end
  end

  def destroy
    @product = Product.unscoped.find(params[:id])

    if @product.mark_as_deleted
      flash[:success] = 'El producto ha sido eliminado.'
    else
      flash[:fail] = 'El producto no ha podido ser eliminado. Intente nuevamente.'
    end

    respond_to do |format|
      format.html { redirect_to admin_products_url }
    end
  end

  def import_products
    begin
      unless params[:file].blank?
        @not_imported = ProductsImporter.import(params[:file])


        @success = true
      end

    rescue
      @error = "Ha ocurrido un error. Por favor, verifique el formato del archivo."
    end

    respond_to do |format|
      format.html
    end

  end

  def search
    keywords = params[:keywords]

    @products = Product.unscoped.not_deleted.search(keywords).paginate(:page => params[:page], :per_page => 50)

    respond_to do |format|
      format.html
    end
  end

  def sold_out
    @product = Product.unscoped.find(params[:product_id])

    respond_to do |format|
      format.html
    end
  end

  def update_sold_out
    @product = Product.unscoped.find(params[:product_id])

    sold_out_colors = params[:color_ids]

    @product.mark_as_sold_out(sold_out_colors)

    respond_to do |format|
      format.html { redirect_to admin_products_url }
    end
  end

  def toggle_display
    product = Product.unscoped.find(params[:product_id])

    unless product.blank?
      if product.display?
        product.update_attribute(:display, 0)
      else
        product.update_attribute(:display, 1)
      end
    end

    respond_to do |format|
      format.html { redirect_to admin_products_url }
    end
  end

  private

  def get_categories
    @categories = Category.all
  end

  def get_colors
    @colors = Color.all
  end

  def get_brands
    @brands = Brand.all
  end

  def get_products_at_home_count
    @products_at_home = Product.at_home.count
  end
end
